package com.zynn.common.core.utils;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisCluster;
import java.util.List;

/**
 * 功能描述: redis存储和获取List集合
 * @author: 刘猛
 * @date: 2018/9/19 10:57
 **/
@Component
public class JedisClientSingle {
    @Autowired
    JedisCluster jedisCluster;
    /**
     * 设置List集合
     * @param key
     * @param list
     */
    public void setList(String key , List<?> list,int validTime){
        try {
            if(!CollectionUtils.isEmpty(list)){
                jedisCluster.setex(key.getBytes(),validTime,SerializeUtil.serializeList(list));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取List集合
     * @param key
     * @return
     */
    public List<?> getList(String key){
        if(jedisCluster == null || !jedisCluster.exists(key)){
            return null;
        }
        byte[] data = jedisCluster.get(key.getBytes());
        return SerializeUtil.unserializeList(data);
    }
}
